﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace ManageBanking.Models
{
    public class T_AccountModels
    {
        OnlineBankingDataContext obdc = new OnlineBankingDataContext();
        TotalMoneyModels tt1 = new TotalMoneyModels();

        public List<TypeAccount> ListAllAccount()
        {
            return obdc.TypeAccounts.ToList();
        }
        public bool CheckExistAcc(string idaccount)
        {
            int i = obdc.Account_Usernames.Where(a => a.IdAccount == idaccount).Count();
            if (i == 0)
                return false;
            else
                return true;
        }
        public void TransferMoney(string FromAccount, string ToAccount, decimal money,string pass)
        {
            Transfer tf = new Transfer();
            tf.IdAccountFrom = FromAccount;
            tf.IdAccountTo = ToAccount;
            tf.DateCreation = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            tf.Money = money;
            tf.Solve = false;
            tf.PassTras = pass;
            obdc.Transfers.InsertOnSubmit(tf);
            obdc.SubmitChanges();



        }
        public bool UpdateReceive(int id)
        {
            bool kq = true;
            try
            {
                Transfer ab = obdc.Transfers.Where(a => a.IdTransfer == id).First();
                ab.Solve = true;
                obdc.SubmitChanges();

            }
            catch
            {
                kq = false;
            
            }
            return kq;

        
        }
        public string getMD5(String str)
        {
            MD5CryptoServiceProvider mdcsp = new MD5CryptoServiceProvider();
            byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
            b = mdcsp.ComputeHash(b);
            StringBuilder s = new StringBuilder();
            foreach (byte by in b)
            {
                s.Append(by.ToString("x2").ToLower());
            }
            return s.ToString();
        }
        public List<InformationAccount> info(int idusername)
        {
            return obdc.InformationAccounts.Where(a => a.IdUsername == idusername).OrderByDescending(a=>a.DateCreate).ToList();

        
        }
        public List<InformationAccount> infoactive(int idusername)
        {
            return obdc.InformationAccounts.Where(a => a.IdUsername == idusername && a.Activation==true).OrderByDescending(a => a.DateCreate).ToList();


        }

        public Username InforUser(string idaccount)
        {
            int iduser = obdc.Account_Usernames.Where(a => a.IdAccount.Equals(idaccount)).Select(a => a.IdUsername).First();
            return obdc.Usernames.Where(a => a.IdUsername == iduser).First();
        
        }

        public List<decimal> TotalMoney(int iduser)
        { 
        List<decimal> tt = new List<decimal>();
        List<InformationAccount> inf = infoactive(iduser);
        for (int i = 0; i < inf.Count; i++)
        {
            tt.Insert(i, tt1.ttmoney(inf[i].IdAccount) );
        }
            
        return tt;
        
        }
        public List<decimal> TotalMoneyNoActive(int iduser)
        {
            List<decimal> tt = new List<decimal>();
            List<InformationAccount> inf = info(iduser);
            for (int i = 0; i < inf.Count; i++)
            {
                tt.Insert(i, tt1.ttmoney(inf[i].IdAccount));
            }

            return tt;

        }

        public List<InformationAccount> infodep(int idusername)
        {
            return obdc.InformationAccounts.Where(a => a.IdUsername == idusername).OrderByDescending(a => a.DateCreate).ToList();


        }
       
        
        public string AccountNumberRamDom()
        {
            Random rd = new Random();
            List<string> Liststr = new List<string>();
            string str = "";
            string number = "";
            for (int i = 0; i < 10; i++)
            {
                Liststr.Add(i.ToString());
            }
            for (int j = 0; j < 16; j++)
            {
                str = Liststr[rd.Next(0, 9)];
                while (number.Where(x => x.ToString() == str).ToList().Count >= 3)
                {
                    str = Liststr[rd.Next(0, 9)];
                }
                number += str;
            }
            return number;
        }

        public int TotalUser(int iduser)
        {
            return obdc.Account_Usernames.Where(a=>a.IdUsername==iduser && a.Activation==true).Count();
        }
        public int TotalUserNoActive(int iduser)
        {
            return obdc.Account_Usernames.Where(a => a.IdUsername == iduser).Count();
        }


        public List<ReceiveMoney> ListReceive(int idusername, out int total)
        {
            total = obdc.ReceiveMoneys.Where(ac => ac.IdUsername==idusername).Count();
            return obdc.ReceiveMoneys.Where(ac => ac.IdUsername == idusername).ToList();
        }

        public bool Cancel(int id)
        {
            bool kq = true;

            try
            {
                OnlineBankingDataContext olbk = new OnlineBankingDataContext();
                Transfer tr = olbk.Transfers.Where(a => a.IdTransfer == id).First();
                olbk.Transfers.DeleteOnSubmit(tr);
                olbk.SubmitChanges();
            }
            catch
            {
                kq = false;
            }
            return kq;
        }
        public List<CancelTransfer> ListCanCel(int idusername, out int total)
        {
            total = obdc.CancelTransfers.Where(ac => ac.IdUsername == idusername).Count();
            return obdc.CancelTransfers.Where(ac => ac.IdUsername == idusername).ToList();
        }

    }
}
